﻿/*
 * 由SharpDevelop创建。
 * 用户： liang
 * 日期: 2016/1/6 星期三
 * 时间: 18:06
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;

namespace VCOM
{
    using System;
    using System.Text;
    using System.Windows.Forms;

    internal class Utils
    {
        public static long getMMSystemTime()
        {
            return DateTime.Now.Ticks;
        }

        public static string getStrSystemTime()
        {
            return DateTime.Now.ToString("[yyyy-MM-dd hh:mm:ss.fff]\r");
        }

        public static string HexToStr(string s)
        {
            s = s.Replace(" ", "");
            byte[] bytes = new byte[s.Length / 2];
            int num = 0;
            for (int i = 0; i < s.Length; i += 2)
            {
                try
                {
                    bytes[num++] = Convert.ToByte(s.Substring(i, 2), 0x10);
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.ToString(), "错误提示");
                }
            }
            return Encoding.Default.GetString(bytes).Replace("\0", "");
        }

        public static byte[] StrHexArrToByte(string s)
        {
            s = s.Replace(" ", "");
            byte[] buffer = new byte[s.Length / 2];
            int num = 0;
            for (int i = 0; i < s.Length; i += 2)
            {
                try
                {
                    buffer[num++] = Convert.ToByte(s.Substring(i, 2), 0x10);
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.ToString(), "错误提示");
                }
            }
            return buffer;
        }

        public static byte StrHexToByte(string str)
        {
            byte num = 0;
            try
            {
                num = Convert.ToByte(str, 0x10);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString(), "错误提示");
            }
            return num;
        }

        public static string StrToHex(string s)
        {
            StringBuilder builder = new StringBuilder();
            byte[] bytes = Encoding.GetEncoding("GB18030").GetBytes(s);
            for (int i = 0; i < bytes.Length; i++)
            {
                builder.Append(Convert.ToString(bytes[i], 0x10).ToUpper().PadLeft(2, '0'));
                builder.Append(" ");
            }
            return builder.ToString();
        }
    }

}
